$(function(){
    var t1 = null;
    // 获取验证码
    $("#acquireCode").click(function(){
        var jsPhone = $(".js-phone").val();
        if (jsPhone == ""){
            alert("请输入手机号码");
        } else if (!(/^1[34578]\d{9}$/.test(jsPhone))) {
            alert("手机格式不对");
        } else {
        	$.post('/sendIdentify',
        		{jsPhone:jsPhone},	
        		function(result){
        		if(result.result=="error"){
        			alert("验证码发送失败,请在60秒后重试");
        		}
        	});
            countdown($(this));
        }
    })

    $(".js-register").click(function(){
        var jsPhone = $(".js-phone").val();
        var jsVerification = $(".js-verification").val();
        if (jsPhone == "" || jsVerification == "") {
            alert("手机或验证码输入错误");
        } else {
        	$.post('/checkIdentify',
            		{jsVerification:jsVerification},	
            		function(result){
            		if(result.result=="success"){
            			window.location.href = "/showIndex?jsPhone="+jsPhone;
            		}else{
            			alert("验证码过期或验证码错误");
            		}
            	});
        }
    })

    function countdown(obj) {
        var count = 60;
        $(obj).css({ background: '#e7e7e7' }).val(count.toString() + "秒").attr('disabled', 'disabled');

        var doCount = function () {
            if (--count < 1) {
                var t2 = clearInterval(t1);
                $(obj).css({ background: '#e7e7e7' }).val('重新发送').removeAttr('disabled');
            }
            else {
                $(obj).val(count.toString() + "秒");
            }
        };

        t1 = setInterval(function () { doCount(); }, 1000);

    }
})